package com.bds.wm.action;

import com.alibaba.fastjson.JSONObject;
import com.bds.wm.model.common.Result;
import com.bds.wm.model.procmodel.*;
import com.bds.wm.service.ShopViewService;
import com.bds.wm.utils.CheckParams;
import com.bds.wm.utils.Const;
import com.google.common.collect.Lists;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.*;

/**
 * Created by DongChenchen on 2018/6/25 0025
 */
@Controller
public class ShopViewAction {
    private static Logger LOGGER = Logger.getLogger(ShopViewAction.class);

    @Resource
    private ShopViewService shopViewService;

    /**
     *品牌品类下拉框
     * @return
     */
    @RequestMapping(value = "/service/data/getAllSubCategory", method = RequestMethod.GET)
    @ResponseBody
    public Result getAllSubCategory() {
        Result result = new Result();
        Map<String,List<String>> brands = shopViewService.getAllSubCategory();
        result.setResult(Const.SUC, brands);
        return result;
    }

    /**
     * 获取店铺下拉框
     * @param start_month
     * @param end_month
     * @param brandName
     * @param subcat_name
     * @return
     */
    @RequestMapping(value = "/service/data/getStoreNameList", method = RequestMethod.GET)
    @ResponseBody
    public Result getStoreNameList(String start_month,
                                   String end_month,String brandName,String subcat_name){
        Result result = new Result();

        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        if (startMonth == null || endMonth == null||brandName == null || subcat_name == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        List<StoreNames> brandOverviewResult =
                shopViewService.getStoreNameList(start_month,end_month,brandName,
                        subcat_name);
        String str=JSONObject.toJSONString(brandOverviewResult);
        Collections.sort(brandOverviewResult, new Comparator<StoreNames>() {
            @Override
            public int compare(StoreNames o1, StoreNames o2) {
                return o1.getCityid().compareTo(o2.getCityid());
            }
        });
        result.setResult(Const.SUC, brandOverviewResult);
        str=JSONObject.toJSONString(brandOverviewResult);
        return result;
    }

    /**
     * 店铺信息表格
     * @param start_month
     * @param end_month
     * @param brandName
     * @param restaurantName
     * @return
     */
    @RequestMapping(value = "/service/data/getStoreOverviewResultbyProduct", method = RequestMethod.GET)
    @ResponseBody
    public Result getStoreOverviewResultbyProduct(String start_month,
                                         String end_month,
                                         String brandName,
                                         String restaurantName){
        Result result = new Result();

        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        if (startMonth == null || endMonth == null||brandName == null || restaurantName == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        List<StoreOverviewResultbyProduct> brandOverviewResult =
                shopViewService.getStoreOverviewResultbyProduct(start_month,end_month,brandName,
                        restaurantName);
        result.setResult(Const.SUC, brandOverviewResult);

        return result;
    }


    /**
     * 获得单店概览数据，同时获取所有竞争店铺，及各个竞争店铺的概览数据
     */
    @RequestMapping(value = "/service/data/getStoreOverviewResult", method = RequestMethod.GET)
    @ResponseBody
    public Result getStoreOverviewResult(String start_month,
                                   String end_month,String brand_name,String restaurant){
        Result result = new Result();
        JSONObject obj=new JSONObject();
        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        if (startMonth == null || endMonth == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        StoreOverviewResult store = shopViewService.getStoreOverviewResult(start_month,end_month,brand_name, restaurant);
        //获取所有竞争店铺
        List<CompetitorShops> compShops = shopViewService.getCompetitoronMap(restaurant,start_month,end_month);
        obj.put("shopOverView",store);
        obj.put("compShops",compShops);
        result.setResult(Const.SUC, obj);
        return result;
    }


    /**
     * 获取竞争店铺表格数据
     */
    @RequestMapping(value = "/service/data/getcomstoreinforesult",method = RequestMethod.GET)
    @ResponseBody
    public Result getcomstoreinfo(String start_month,
                                  String end_month,String brand_name,String restaurant){
        Result result = new Result();
        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        if (startMonth == null || endMonth == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        List<StoreOverviewResultComp>  compShops = shopViewService.getcomstoreinfo(start_month,end_month,restaurant);
        List<StoreOverviewResultRateComp> compShopRates = shopViewService.getcomstoreyoyinfo(start_month,end_month,brand_name,restaurant);
        JSONObject jsonObject = new JSONObject();
        for(int i=0;i<compShops.size();i++){
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("sales",compShops.get(i));
            jsonObject.put(compShops.get(i).getRestaurantName(),jsonObject1);
        }
        for(int i=0;i<compShopRates.size();i++){
            JSONObject jsonObject1 = jsonObject.getJSONObject(compShopRates.get(i).getRestaurantName());
            jsonObject1.put("yoy",compShopRates.get(i));
            jsonObject.put(compShopRates.get(i).getRestaurantName(),jsonObject1);
        }
        result.setResult(Const.SUC, jsonObject);
        return result;
    }
    /**
     * 获得同比、环比增长率
     */
    @RequestMapping(value = "/service/data/getStoreOverviewResultRate", method = RequestMethod.GET)
    @ResponseBody
    public Result getStoreOverviewResultRate(String start_month,
                                         String end_month,String brand_name,String restaurant){
        Result result = new Result();

        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        if (startMonth == null || endMonth == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        StoreOverviewResultRate store = shopViewService.getStoreOverviewResultRate(startMonth,endMonth,brand_name, restaurant);

        if (store!=null)
            result.setResult(Const.SUC, store);
        else
            result.setCode(Const.FAIL);
        return result;
    }

    /**
     * 词云图
     * @param end_month
     * @return
     */
    @RequestMapping(value = "/service/data/getStoreKeyWord", method = RequestMethod.GET)
    @ResponseBody
    public Result getStoreKeyWord(String end_month,String storeName,String brandName,String start_month){
        Result result = new Result();

        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        try {
            storeName = URLDecoder.decode(storeName, "utf-8");
        } catch (UnsupportedEncodingException e) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        if (endMonth == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        List<StoreKeyWord> brandMonthlyRatingTrend=shopViewService.getStoreKeyWord(end_month,
                storeName , brandName, start_month);
        result.setResult(Const.SUC, brandMonthlyRatingTrend);

        return result;
    }

    /**
     * 下拉框   根据店铺获取品类和品牌
     * @param end_month
     * @param restaurantID
     * @param start_month
     * @return
     */
    @RequestMapping(value = "/service/data/getStoreBrandList", method = RequestMethod.GET)
    @ResponseBody
    public Result getStoreBrandList(String end_month,String restaurantID,String start_month){
        Result result = new Result();

        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        if (endMonth == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        StoreBrand brandMonthlyRatingTrend=shopViewService.getStoreBrandList(end_month,
                restaurantID , start_month);
        result.setResult(Const.SUC, brandMonthlyRatingTrend);

        return result;
    }

    /**
     * 获得月度满意趋势
     */
    @RequestMapping(value = "/service/data/getStoreMonthlyRatingTrend", method = RequestMethod.GET)
    @ResponseBody
    public Result getStoreMonthlyRatingTrend(String end_month,String brand_name,String restaurant){
        Result result = new Result();

        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        if (endMonth == null||restaurant==null) {
            LOGGER.info("参数异常!");
            result.setResult(Const.FAIL, "参数异常!");
            return result;
        }
        List<ShopTrend> list = shopViewService.getStoreMonthlyTrend(endMonth,brand_name,restaurant);

        if (list!=null)
            result.setResult(Const.SUC, list);
        else
            result.setCode(Const.FAIL);
        return result;
    }

    /**
     * 查询所有竞争店铺
     */
    @RequestMapping(value = "/service/data/getCompetitoronMap", method = RequestMethod.GET)
    @ResponseBody
    public Result getCompetitoronMap(String storeName,String start_month,String end_month) {
        Result result = new Result();

        if (storeName==null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        List<CompetitorShops> list = shopViewService.getCompetitoronMap(storeName,start_month,end_month);

        if (list!=null)
            result.setResult(Const.SUC, list);
        else
            result.setCode(Const.FAIL);
        return result;
    }

}
